package com.itheima.tlias08_14.controller;

import com.alibaba.fastjson.JSONObject;
import com.itheima.tlias08_14.config.StudentConfig;
import com.itheima.tlias08_14.dto.PageClazzDTO;
import com.itheima.tlias08_14.entity.Department;
import com.itheima.tlias08_14.utils.ResultUtil;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class TestReadConfigByYml {

    private static final Logger log = LoggerFactory.getLogger(TestReadConfigByYml.class);
    //@Value只能读取配置文件中八种基本数据类型和数组 不能读集合
    @Value("${student.name}")
    String name;
    @Value("${student.subjects}")
    String[] subjects;

    @Resource
    Environment env;

    @Resource
    StudentConfig config;

    @Resource(name = "dept")
    Department de;

    @Autowired(required = false)
    PageClazzDTO pageClazzDTO;

    @GetMapping("/readConfig")
    public ResultUtil readConfig() {
        JSONObject jO = new JSONObject();
        jO.put("name", name);
        jO.put("subjects1", subjects);
        jO.put("name1", config.getName());
        jO.put("subjects", config.getSubjects());
        jO.put("teachers", config.getTeachers());
        jO.put("family", config.getFamily());

        //使用Spring内置的Environment对象读取配置文件
        //getActiveProfiles()获取当前激活的配置文件名
        String[] activeProfiles = env.getActiveProfiles();
        jO.put("activeProfiles", activeProfiles);
        jO.put("serverPort", env.getProperty("server.port"));
        log.info("department = {}",de);
        log.info("pageClazzDTO = {}",pageClazzDTO);
        return ResultUtil.success(jO);
    }
}
